<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>basicstat.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_stats_basicstat.js.html">basicstat.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A basic statistics tracker.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.stats.BasicStat&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.array&#39;);
<a name="line23"></a>goog.require(&#39;goog.iter&#39;);
<a name="line24"></a>goog.require(&#39;goog.log&#39;);
<a name="line25"></a>goog.require(&#39;goog.object&#39;);
<a name="line26"></a>goog.require(&#39;goog.string.format&#39;);
<a name="line27"></a>goog.require(&#39;goog.structs.CircularBuffer&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Tracks basic statistics over a specified time interval.
<a name="line33"></a> *
<a name="line34"></a> * Statistics are kept in a fixed number of slots, each representing
<a name="line35"></a> * an equal portion of the time interval.
<a name="line36"></a> *
<a name="line37"></a> * Most methods optionally allow passing in the current time, so that
<a name="line38"></a> * higher level stats can synchronize operations on multiple child
<a name="line39"></a> * objects.  Under normal usage, the default of goog.now() should be
<a name="line40"></a> * sufficient.
<a name="line41"></a> *
<a name="line42"></a> * @param {number} interval The stat interval, in milliseconds.
<a name="line43"></a> * @constructor
<a name="line44"></a> * @final
<a name="line45"></a> */
<a name="line46"></a>goog.stats.BasicStat = function(interval) {
<a name="line47"></a>  goog.asserts.assert(interval &gt; 50);
<a name="line48"></a>
<a name="line49"></a>  /**
<a name="line50"></a>   * The time interval that this statistic aggregates over.
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.interval_ = interval;
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * The number of milliseconds in each slot.
<a name="line58"></a>   * @type {number}
<a name="line59"></a>   * @private
<a name="line60"></a>   */
<a name="line61"></a>  this.slotInterval_ = Math.floor(interval / goog.stats.BasicStat.NUM_SLOTS_);
<a name="line62"></a>
<a name="line63"></a>  /**
<a name="line64"></a>   * The array of slots.
<a name="line65"></a>   * @type {goog.structs.CircularBuffer}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.slots_ =
<a name="line69"></a>      new goog.structs.CircularBuffer(goog.stats.BasicStat.NUM_SLOTS_);
<a name="line70"></a>};
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * The number of slots. This value limits the accuracy of the get()
<a name="line75"></a> * method to (this.interval_ / NUM_SLOTS).  A 1-minute statistic would
<a name="line76"></a> * be accurate to within 2 seconds.
<a name="line77"></a> * @type {number}
<a name="line78"></a> * @private
<a name="line79"></a> */
<a name="line80"></a>goog.stats.BasicStat.NUM_SLOTS_ = 50;
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * @type {goog.log.Logger}
<a name="line85"></a> * @private
<a name="line86"></a> */
<a name="line87"></a>goog.stats.BasicStat.prototype.logger_ =
<a name="line88"></a>    goog.log.getLogger(&#39;goog.stats.BasicStat&#39;);
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * @return {number} The interval which over statistics are being
<a name="line93"></a> *     accumulated, in milliseconds.
<a name="line94"></a> */
<a name="line95"></a>goog.stats.BasicStat.prototype.getInterval = function() {
<a name="line96"></a>  return this.interval_;
<a name="line97"></a>};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Increments the count of this statistic by the specified amount.
<a name="line102"></a> *
<a name="line103"></a> * @param {number} amt The amount to increase the count by.
<a name="line104"></a> * @param {number=} opt_now The time, in milliseconds, to be treated
<a name="line105"></a> *     as the &quot;current&quot; time.  The current time must always be greater
<a name="line106"></a> *     than or equal to the last time recorded by this stat tracker.
<a name="line107"></a> */
<a name="line108"></a>goog.stats.BasicStat.prototype.incBy = function(amt, opt_now) {
<a name="line109"></a>  var now = opt_now ? opt_now : goog.now();
<a name="line110"></a>  this.checkForTimeTravel_(now);
<a name="line111"></a>  var slot = /** @type {goog.stats.BasicStat.Slot_} */ (this.slots_.getLast());
<a name="line112"></a>  if (!slot || now &gt;= slot.end) {
<a name="line113"></a>    slot = new goog.stats.BasicStat.Slot_(this.getSlotBoundary_(now));
<a name="line114"></a>    this.slots_.add(slot);
<a name="line115"></a>  }
<a name="line116"></a>  slot.count += amt;
<a name="line117"></a>  slot.min = Math.min(amt, slot.min);
<a name="line118"></a>  slot.max = Math.max(amt, slot.max);
<a name="line119"></a>};
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/**
<a name="line123"></a> * Returns the count of the statistic over its configured time
<a name="line124"></a> * interval.
<a name="line125"></a> * @param {number=} opt_now The time, in milliseconds, to be treated
<a name="line126"></a> *     as the &quot;current&quot; time.  The current time must always be greater
<a name="line127"></a> *     than or equal to the last time recorded by this stat tracker.
<a name="line128"></a> * @return {number} The total count over the tracked interval.
<a name="line129"></a> */
<a name="line130"></a>goog.stats.BasicStat.prototype.get = function(opt_now) {
<a name="line131"></a>  return this.reduceSlots_(opt_now,
<a name="line132"></a>      function(sum, slot) { return sum + slot.count; },
<a name="line133"></a>      0);
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Returns the magnitute of the largest atomic increment that occurred
<a name="line139"></a> * during the watched time interval.
<a name="line140"></a> * @param {number=} opt_now The time, in milliseconds, to be treated
<a name="line141"></a> *     as the &quot;current&quot; time.  The current time must always be greater
<a name="line142"></a> *     than or equal to the last time recorded by this stat tracker.
<a name="line143"></a> * @return {number} The maximum count of this statistic.
<a name="line144"></a> */
<a name="line145"></a>goog.stats.BasicStat.prototype.getMax = function(opt_now) {
<a name="line146"></a>  return this.reduceSlots_(opt_now,
<a name="line147"></a>      function(max, slot) { return Math.max(max, slot.max); },
<a name="line148"></a>      Number.MIN_VALUE);
<a name="line149"></a>};
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Returns the magnitute of the smallest atomic increment that
<a name="line154"></a> * occurred during the watched time interval.
<a name="line155"></a> * @param {number=} opt_now The time, in milliseconds, to be treated
<a name="line156"></a> *     as the &quot;current&quot; time.  The current time must always be greater
<a name="line157"></a> *     than or equal to the last time recorded by this stat tracker.
<a name="line158"></a> * @return {number} The minimum count of this statistic.
<a name="line159"></a> */
<a name="line160"></a>goog.stats.BasicStat.prototype.getMin = function(opt_now) {
<a name="line161"></a>  return this.reduceSlots_(opt_now,
<a name="line162"></a>      function(min, slot) { return Math.min(min, slot.min); },
<a name="line163"></a>      Number.MAX_VALUE);
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Passes each active slot into a function and accumulates the result.
<a name="line169"></a> *
<a name="line170"></a> * @param {number|undefined} now The current time, in milliseconds.
<a name="line171"></a> * @param {function(number, goog.stats.BasicStat.Slot_): number} func
<a name="line172"></a> *     The function to call for every active slot.  This function
<a name="line173"></a> *     takes two arguments: the previous result and the new slot to
<a name="line174"></a> *     include in the reduction.
<a name="line175"></a> * @param {number} val The initial value for the reduction.
<a name="line176"></a> * @return {number} The result of the reduction.
<a name="line177"></a> * @private
<a name="line178"></a> */
<a name="line179"></a>goog.stats.BasicStat.prototype.reduceSlots_ = function(now, func, val) {
<a name="line180"></a>  now = now || goog.now();
<a name="line181"></a>  this.checkForTimeTravel_(now);
<a name="line182"></a>  var rval = val;
<a name="line183"></a>  var start = this.getSlotBoundary_(now) - this.interval_;
<a name="line184"></a>  for (var i = this.slots_.getCount() - 1; i &gt;= 0; --i) {
<a name="line185"></a>    var slot = /** @type {goog.stats.BasicStat.Slot_} */ (this.slots_.get(i));
<a name="line186"></a>    if (slot.end &lt;= start) {
<a name="line187"></a>      break;
<a name="line188"></a>    }
<a name="line189"></a>    rval = func(rval, slot);
<a name="line190"></a>  }
<a name="line191"></a>  return rval;
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Computes the end time for the slot that should contain the count
<a name="line197"></a> * around the given time.  This method ensures that every bucket is
<a name="line198"></a> * aligned on a &quot;this.slotInterval_&quot; millisecond boundary.
<a name="line199"></a> * @param {number} time The time to compute a boundary for.
<a name="line200"></a> * @return {number} The computed boundary.
<a name="line201"></a> * @private
<a name="line202"></a> */
<a name="line203"></a>goog.stats.BasicStat.prototype.getSlotBoundary_ = function(time) {
<a name="line204"></a>  return this.slotInterval_ * (Math.floor(time / this.slotInterval_) + 1);
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Checks that time never goes backwards.  If it does (for example,
<a name="line210"></a> * the user changes their system clock), the object state is cleared.
<a name="line211"></a> * @param {number} now The current time, in milliseconds.
<a name="line212"></a> * @private
<a name="line213"></a> */
<a name="line214"></a>goog.stats.BasicStat.prototype.checkForTimeTravel_ = function(now) {
<a name="line215"></a>  var slot = /** @type {goog.stats.BasicStat.Slot_} */ (this.slots_.getLast());
<a name="line216"></a>  if (slot) {
<a name="line217"></a>    var slotStart = slot.end - this.slotInterval_;
<a name="line218"></a>    if (now &lt; slotStart) {
<a name="line219"></a>      goog.log.warning(this.logger_, goog.string.format(
<a name="line220"></a>          &#39;Went backwards in time: now=%d, slotStart=%d.  Resetting state.&#39;,
<a name="line221"></a>          now, slotStart));
<a name="line222"></a>      this.reset_();
<a name="line223"></a>    }
<a name="line224"></a>  }
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * Clears any statistics tracked by this object, as though it were
<a name="line230"></a> * freshly created.
<a name="line231"></a> * @private
<a name="line232"></a> */
<a name="line233"></a>goog.stats.BasicStat.prototype.reset_ = function() {
<a name="line234"></a>  this.slots_.clear();
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/**
<a name="line240"></a> * A struct containing information for each sub-interval.
<a name="line241"></a> * @param {number} end The end time for this slot, in milliseconds.
<a name="line242"></a> * @constructor
<a name="line243"></a> * @private
<a name="line244"></a> */
<a name="line245"></a>goog.stats.BasicStat.Slot_ = function(end) {
<a name="line246"></a>  /**
<a name="line247"></a>   * End time of this slot, exclusive.
<a name="line248"></a>   * @type {number}
<a name="line249"></a>   */
<a name="line250"></a>  this.end = end;
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * Aggregated count within this slot.
<a name="line256"></a> * @type {number}
<a name="line257"></a> */
<a name="line258"></a>goog.stats.BasicStat.Slot_.prototype.count = 0;
<a name="line259"></a>
<a name="line260"></a>
<a name="line261"></a>/**
<a name="line262"></a> * The smallest atomic increment of the count within this slot.
<a name="line263"></a> * @type {number}
<a name="line264"></a> */
<a name="line265"></a>goog.stats.BasicStat.Slot_.prototype.min = Number.MAX_VALUE;
<a name="line266"></a>
<a name="line267"></a>
<a name="line268"></a>/**
<a name="line269"></a> * The largest atomic increment of the count within this slot.
<a name="line270"></a> * @type {number}
<a name="line271"></a> */
<a name="line272"></a>goog.stats.BasicStat.Slot_.prototype.max = Number.MIN_VALUE;
</pre>


</body>
</html>
